var t = require("../../../@babel/runtime/helpers/defineProperty"), e = require("../../../mycomponent/yz_pay/yz_pay"), a = getApp();

Page({
    behaviors: [ e ],
    data: {
        language: "",
        agreementPayData: "",
        agreementPay: !1,
        agreementPayShow: !1,
        store_id: "",
        confirmOrderStatus: !1,
        payMuch: "0",
        realname: "",
        mobile: "",
        popupCouponSpecs: !1,
        checkCouponList: [],
        coupon_size: 0,
        use_coupon_size: 0,
        coupons: [],
        cup_notice: !0,
        isShowCoupon: !1,
        order_ids: "",
        income: {
            money_total: 0,
            sure_withdraw_money: 0
        },
        store_set: {
            is_cash_pay: "",
            is_open_cashier: "",
            is_write_information: ""
        },
        payPrice: 0,
        checkDeductionList: [],
        form: {
            mobile: "",
            realname: ""
        },
        allData: {},
        order_data: "",
        loadDiscount: !1,
        goods_price: 0,
        dispatch_price: 0,
        discount_price: 0,
        price: 0,
        deduction_price: 0,
        payType: [],
        typename: "",
        show1: !1,
        clicktag: 0,
        uid: "",
        cashier_type: "",
        hotel_id: "",
        order_pay_id: "",
        showPacket: !1,
        redirectData: {},
        balanceNow_store: 0
    },
    onLoad: function(t) {
        var e = [];
        if (t.scene) {
            var i = decodeURIComponent(t.scene);
            if (console.log(i), i) {
                e = i.split(",");
                for (var s = 0; s < e.length; s++) {
                    var o;
                    o = e[s].split("="), console.log(o), "store_id" == o[0] && this.setData({
                        store_id: o[1]
                    }), "hotel_id" == o[0] && this.setData({
                        hotel_id: o[1]
                    }), "mid" == o[0] && a._setMid(o[1]);
                }
            }
        }
        if (t.q) {
            var n = decodeURIComponent(t.q);
            if (console.log(n), n) {
                var r = decodeURIComponent(t.q);
                if (r) {
                    if ("-1" == r.indexOf("?i=")) e = r.split(","); else e = r.split("&")[1].split(",");
                    for (var d = 0; d < e.length; d++) {
                        var c;
                        "store_id" == (c = e[d].split("="))[1] && this.setData({
                            store_id: c[2]
                        }), "mid" == c[0] && a._setMid(c[1]);
                    }
                }
            }
        }
        t.hotel_id && this.setData({
            hotel_id: t.hotel_id
        }), a._isTextEmpty(this.data.hotel_id) ? this.setData({
            cashier_type: "store"
        }) : this.setData({
            cashier_type: "hotel"
        }), t.store_id && this.setData({
            store_id: t.store_id
        }), t.cashier_type && this.setData({
            cashier_type: t.cashier_type
        }), "scanning_payment" == this.data.cashier_type ? (this.setData({
            payMuch: t.price,
            show1: !0,
            showPage: !0
        }), wx.setNavigationBarTitle({
            title: "扫码下单"
        }), this.preGoodsBuy()) : this.getCenter();
    },
    onReady: function() {
        var t = wx.getStorageSync("langIndex");
        this.setData({
            language: t.en
        });
    },
    onShow: function() {},
    onShareAppMessage: function() {
        var t = "/packageC/cashier/cashier_pay/cashier_pay";
        return this.data.store_id && (t = "".concat(t, "?store_id=").concat(this.data.store_id)), 
        this.data.hotel_id && (t = "".concat(t, "?hotel_id=").concat(this.data.hotel_id)), 
        {
            path: t
        };
    },
    fromChild: function(t) {
        if ("0.00" == this.data.money || "0.00" == t.detail.money) this.submit("54"); else {
            var e = t.detail.submitArr;
            this.submit(e);
        }
    },
    onClickHide: function() {
        this.setData({
            showPacket: !1
        }), this.toRedirect();
    },
    toPacket: function() {
        wx.redirectTo({
            url: "/packageE/ConsumePacket/ConsumePacketSuccess/ConsumePacketSuccess?order_id=" + this.data.order_ids.toString()
        });
    },
    checkoutConsumePacket: function() {
        var t = this;
        this.getVoiceSet();
        var e = wx.getStorageSync("yz_basic_info");
        if (e && 1 == e.globalParameter.consume_red_packet_status) {
            var i = a.getNetAddresss("plugin.consume-red-packet.Frontend.Modules.Packet.Controllers.has.index");
            a._postNetWork({
                url: i,
                data: {
                    order_id: this.data.order_ids.split(",")
                },
                success: function(e) {
                    1 == e.data.result ? t.setData({
                        show1: !1,
                        showPacket: !0
                    }) : t.toRedirect();
                },
                fail: function(e) {
                    t.toRedirect(), console.log(e);
                }
            });
        } else this.toRedirect();
    },
    toRedirect: function() {
        wx.showToast({
            icon: "none",
            title: "支付成功",
            duration: 2e3
        }), this.data.order_sn && this.payCallBack();
    },
    getVoiceSet: function() {
        var t = a.getNetAddresss("plugin.store-cashier.frontend.cashier.voice.index");
        a._postNetWork({
            url: t,
            data: {
                order_id: this.data.order_ids
            },
            success: function(t) {
                var e = t.data;
                if (1 == e.result) {
                    var a = wx.getBackgroundAudioManager();
                    a.title = "支付成功语音提示", a.src = e.data.url, a.play();
                } else console.log(e);
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    PayShowbtn: function() {
        this.setData({
            agreementPayShow: !1
        });
    },
    initStore: function(t) {
        this.setData({
            confirmOrderStatus: !1,
            payMuch: "0",
            realname: "",
            mobile: "",
            popupCouponSpecs: !1,
            order_ids: ""
        }), "hotel" === this.data.cashier_type || (this.setData({
            store: t.store,
            store_set: t.store_set,
            income: t.income
        }), Number(t.store_set.every_min_money) && this.setData({
            payMuch: t.store_set.every_min_money
        }), 1 != t.store_set.is_open_cashier && a.confirm("该门店没有开启收银台", function() {
            wx.navigateBack({
                delta: 1,
                fail: function() {
                    wx.reLaunch({
                        url: "/packageG/index/index"
                    });
                }
            });
        }, !1));
    },
    _handleKeyPress: function(t) {
        var e = t.currentTarget.dataset.num;
        if (-1 == e) return !1;
        switch (String(e)) {
          case ".":
            this._handleDecimalPoint();
            break;

          case "D":
            this._handleDeleteKey();
            break;

          case "S":
            this._handleConfirmKey();
            break;

          default:
            this._handleNumberKey(e);
        }
    },
    _handleDecimalPoint: function() {
        if (this.data.payMuch.indexOf(".") > -1) return !1;
        this.data.payMuch.length ? this.setData({
            payMuch: this.data.payMuch + "."
        }) : this.setData({
            payMuch: "0."
        });
    },
    _handleDeleteKey: function() {
        var t = this.data.payMuch;
        t.length <= 1 ? this.setData({
            payMuch: "0"
        }) : this.setData({
            payMuch: t.substring(0, t.length - 1)
        });
    },
    _handleConfirmKey: function() {
        var t = this.data.payMuch;
        if (!t.length || 0 === Number(t)) return wx.showToast({
            icon: "none",
            title: "您目前未输入!",
            duration: 1500
        }), !1;
        t.indexOf(".") > -1 && t.indexOf(".") === t.length - 1 && (t = Number(t.substring(0, t.length - 1)).toFixed(2)), 
        t = Number(t).toFixed(2), this.setData({
            payMuch: t
        }), this.confirmOrder();
    },
    confirmOrder: function() {
        var t = this.data.payMuch;
        t && t > 0 ? (this.setData({
            payPrice: t,
            confirmOrderStatus: !0,
            isdisabled: !0
        }), this.preGoodsBuy()) : (wx.showToast({
            icon: "none",
            title: "请输入正确的付款金额!",
            duration: 1500
        }), this.setData({
            confirmOrderStatus: !1,
            isdisabled: !1
        }));
    },
    _handleNumberKey: function(t) {
        var e = this.data.payMuch;
        if (e.indexOf(".") > -1 && e.substring(e.indexOf(".") + 1).length < 2 && this.setData({
            payMuch: e + t
        }), !(e.indexOf(".") > -1)) if (0 == t && 0 == e.length) this.setData({
            payMuch: "0."
        }); else {
            if (e.length && 0 === Number(e.charAt(0))) return void this.setData({
                payMuch: t
            });
            this.setData({
                payMuch: e + t
            });
        }
    },
    preGoodsBuy: function() {
        var t = this, e = "";
        "hotel" == this.data.cashier_type ? (e += a.getNetAddresss("plugin.hotel.frontend.Order.goods-buy"), 
        e += "&price=" + this.data.payPrice, e += "&hotel_id=" + this.data.hotel_id, e += "&member_coupon_ids=" + this.assembleCoupons(), 
        e += "&orders=" + JSON.stringify(this.assembleDeduction()), e += "&mobile=" + this.data.form.mobile, 
        e += "&realname=" + this.data.form.realname) : "scanning_payment" == this.data.cashier_type ? (e += a.getNetAddresss("plugin.sweep-buy.frontend.controllers.goods-buy.index"), 
        e += "&member_coupon_ids=" + this.assembleCoupons(), e += "&price=" + this.data.payMuch, 
        e += "&goods_id=" + this.options.goods_id, e += "&orders=" + JSON.stringify(this.assembleDeduction())) : (e += a.getNetAddresss("plugin.store-cashier.frontend.Order.goods-buy"), 
        e += "&price=" + this.data.payPrice, e += "&store_id=" + this.data.store_id, e += "&member_coupon_ids=" + this.assembleCoupons(), 
        e += "&orders=" + JSON.stringify(this.assembleDeduction()), e += "&mobile=" + this.data.form.mobile, 
        e += "&realname=" + this.data.form.realname), 0 == this.data.clicktag && (this.setData({
            clicktag: 1
        }), a._getNetWork({
            url: e,
            success: function(e) {
                var a = e.data;
                1 == a.result ? (a.data.balance && t.setData({
                    balanceNow: a.data.balance
                }), "store" == t.data.cashier_type && a.data.store_balance && t.setData({
                    balanceNow_store: a.data.store_balance
                }), t.setViewData(a.data), t.initCoupon(a.data.discount), t.initPayType()) : wx.showModal({
                    title: "提示",
                    content: a.msg,
                    showCancel: !1,
                    success: function(t) {
                        t.confirm && wx.navigateBack({
                            delta: 1
                        });
                    }
                }), setTimeout(function() {
                    t.setData({
                        clicktag: 0
                    });
                }, 1e3);
            },
            fail: function(t) {
                console.log(t);
            }
        }));
    },
    assembleCoupons: function() {
        for (var t = [], e = 0; e < this.data.checkCouponList.length; e++) if (this.data.is_coupon_SELE) {
            var a = this.data.checkCouponList[e].has_conpon_id;
            t = t.concat(a.slice(0, this.data.coupons_temp[this.data.checkCouponList[e].coupon_id].num)), 
            this.setData({
                use_coupon_size: t.length
            });
        } else t.push(this.data.checkCouponList[e].id);
        return t;
    },
    assembleDeduction: function() {
        for (var t = [], e = 0; e < this.data.checkDeductionList.length; e++) t.push(this.data.checkDeductionList[e]);
        return t;
    },
    setViewData: function(t) {
        this.setData({
            allData: t,
            order_data: t.orders,
            loadDiscount: !0,
            goods_price: t.total_goods_price,
            dispatch_price: t.total_dispatch_price,
            deduction_price: t.total_deduction_price,
            price: t.total_price,
            service: t.orders[0].order_fees,
            datas: t.orders[0]
        }), "hotel" != this.data.cashier_type && this.setData({
            discount_price: t.total_discount_price
        });
    },
    initCoupon: function(t) {
        t.deduction_lang && this.setData({
            deduction_lang: t.deduction_lang
        }), this.setData({
            isShowCoupon: "1" == t.whether_show_coupon
        });
        var e = t.member_coupons;
        if (this.setData({
            is_coupon_SELE: "1" == t.coupon_show
        }), this.setData({
            coupon_size: e.length
        }), null != e && "" != e && null != e) if (this.data.is_coupon_SELE) {
            var a = e.sort(function(t, e) {
                return t.expired_at - e.expired_at;
            });
            this.initSelectCoupon(a);
        } else this.setData({
            coupons: e
        });
    },
    defaultSelectCoupon: function(t) {
        var e = [];
        t.forEach(function(t, a) {
            1 == t.checked && e.push(t);
        }), this.setData({
            coupon_size: t.length,
            coupons: t,
            use_coupon_size: e.length,
            checkCouponList: e
        }), console.log(this.data.coupons);
    },
    initSelectCoupon: function(t) {
        for (var e = t, a = [], i = {}, s = 0; s < e.length; s++) {
            var o = 0, n = 0, r = [ e[s].id ];
            e[s].checked && o++, e[s].valid && n++;
            for (var d = s + 1; d < e.length; d++) e[s].coupon_id == e[d].coupon_id && (e[d].checked && o++, 
            e[d].valid && n++, r.push(e[d].id), e.splice(d, 1), d--);
            var c = e[s];
            c.has_conpon_id = r, c.valid_num = n, a.push(c), i[e[s].coupon_id] = {}, i[e[s].coupon_id].num = o;
        }
        this.setData({
            coupons_temp: i,
            coupons: a
        }), console.log(this.data.coupons, this.data.coupons_temp);
    },
    changeConpon: function(t) {
        var e = this, a = t.target.dataset.item, i = t.detail, s = t.target.dataset.index;
        if (i > a.valid_num) return wx.showToast({
            icon: "none",
            title: "使用张数已达上限",
            duration: 1e3
        }), clearTimeout(this.data.timer), void (this.data.timer = setTimeout(function() {
            e.data.coupons_temp[a.coupon_id].num = a.valid_num, e.setData({
                coupons_temp: e.data.coupons_temp
            });
        }, 200));
        if ("" != i && null != i && 0 != i) {
            clearTimeout(this.data.timer), this.data.coupons_temp[a.coupon_id].num = i, this.setData({
                coupons_temp: this.data.coupons_temp
            });
            var o = {
                detail: !0,
                currentTarget: {
                    dataset: {
                        index: s,
                        item: a
                    }
                }
            };
            this.data.timeoutId && clearTimeout(this.data.timeoutId), this.data.timeoutId = setTimeout(function() {
                e.selectCoupon(o);
            }, 1e3);
        } else wx.showToast({
            icon: "none",
            title: "暂不使用请取消勾选",
            duration: 1e3
        });
    },
    initPayType: function() {
        var t = this, e = "";
        "hotel" === this.data.cashier_type ? (e += a.getNetAddresss("plugin.hotel.frontend.payment.list"), 
        e += "&hotel_id=" + this.data.hotel_id, e += "&price=" + this.data.payMuch) : "scanning_payment" == this.data.cashier_type ? (e += a.getNetAddresss("plugin.sweep-buy.frontend.controllers.payment.list"), 
        e += "&price=" + this.data.payMuch) : (e += a.getNetAddresss("plugin.store-cashier.frontend.payment.list"), 
        e += "&store_id=" + this.data.store_id, e += "&price=" + this.data.payMuch), a._getNetWork({
            url: e,
            success: function(e) {
                var a = e.data;
                if (1 == a.result) {
                    var i = a.data, s = {};
                    s.buttons = a.data, t.hanleIcon(s), t.setData({
                        payType: i,
                        typename: "支付",
                        show1: !0
                    });
                } else wx.showToast({
                    title: a.msg,
                    icon: "none",
                    duration: 1500,
                    success: function() {
                        setTimeout(function() {
                            wx.navigateBack({
                                delta: 1
                            });
                        }, 1500);
                    }
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    btnstyle: function(t) {
        return 1 == t || 6 == t || 9 == t || 12 == t || 20 == t || 22 == t ? "#64b42e" : 2 == t || 7 == t || 10 == t || 15 == t || 21 == t ? "#069ce7" : 3 == t ? "#fc992c" : 14 == t ? "#e36724" : 18 == t ? "#dedede" : "#fac337";
    },
    btnclass: function(t) {
        return 1 == t || 6 == t || 9 == t || 12 == t || 20 == t || 22 == t ? "icon-pay_b" : 2 == t || 7 == t || 10 == t || 15 == t || 21 == t ? "icon-pay_c" : 3 == t ? "icon-pay_d" : 14 == t ? "icon-pay_f" : 18 == t ? "card" : "icon-pay_a";
    },
    show1btn: function() {
        this.setData({
            show1: !1
        });
    },
    discountHandle: function(t) {
        var e = t.detail, a = t.currentTarget.dataset.d.code, i = t.currentTarget.dataset.index;
        console.log(this.data.order_data[0], this.data.order_data[0].order_deductions[i]), 
        this.screenDiscount(this.data.order_data[0], {
            checked: e,
            code: a
        }), this.preGoodsBuy();
    },
    screenDiscount: function(t, e) {
        this.deductionListHandle(t, e);
    },
    deductionListHandle: function(t, e) {
        var a = this.data.checkDeductionList, i = !1;
        a.length > 0 && a.forEach(function(a, s) {
            a && a.pre_id == t.pre_id && (i = !0, e && e.checked ? a.deduction_ids && -1 == a.deduction_ids.indexOf(e.code) && a.deduction_ids.push(e.code) : e && !e.checked && a.deduction_ids && a.deduction_ids.indexOf(e.code) > -1 && a.deduction_ids.splice(a.deduction_ids.indexOf(e.code), 1));
        }), i || a.push({
            deduction_ids: e && e.code ? [ e.code ] : [],
            pre_id: t.pre_id
        }), this.setData({
            checkDeductionList: a
        });
    },
    showCoupon: function() {
        0 != this.data.coupon_size ? this.setData({
            popupCouponSpecs: !0
        }) : wx.showToast({
            icon: "none",
            title: "暂无优惠券使用",
            duration: 1500
        });
    },
    selectCoupon: function(e) {
        var a = e.detail, i = e.currentTarget.dataset.index;
        this.setData(t({}, "coupons[" + i + "].checked", a)), this.screenCoupon(a, this.data.coupons[i], e.type), 
        this.setCouponView(), this.preGoodsBuy();
    },
    screenCoupon: function(t, e, a) {
        var i = e, s = this.data.checkCouponList;
        if (this.data.checkCouponList.length > 0) for (var o = 0; o < this.data.checkCouponList.length; o++) this.data.checkCouponList[o].id == i.id && s.splice(o, 1);
        this.data.is_coupon_SELE && a && (this.data.coupons_temp[i.coupon_id].num = t ? 1 : 0, 
        this.setData({
            coupons_temp: this.data.coupons_temp
        })), t ? s.push(i) : this.setData({
            cup_notice: !0
        }), this.setData({
            checkCouponList: s
        });
    },
    setCouponView: function() {
        this.setData({
            use_coupon_size: this.data.checkCouponList.length
        });
    },
    couponclose: function() {
        this.setData({
            popupCouponSpecs: !1
        });
    },
    realnameinp: function(t) {
        var e = t.detail.value;
        this.setData({
            "form.realname": e
        });
    },
    mobileinp: function(t) {
        var e = t.detail.value;
        this.setData({
            "form.mobile": e
        });
    },
    agreementPayinp: function(t) {
        var e = t.detail;
        this.setData({
            agreementPay: e
        });
    },
    showPay: function() {
        this.setData({
            agreementPayShow: !0
        });
    },
    submit: function(t) {
        var e = t, i = this, s = "";
        if ("hotel" == this.data.cashier_type ? (s += a.getNetAddresss("plugin.hotel.frontend.Order.create"), 
        s += "&price=" + this.data.payMuch, s += "&hotel_id=" + this.data.hotel_id, s += "&member_coupon_ids=", 
        s += "&orders=", s += "&mobile=" + this.data.form.mobile, s += "&realname=" + this.data.form.realname) : "scanning_payment" == this.data.cashier_type ? (s += a.getNetAddresss("plugin.sweep-buy.frontend.controllers.create.index"), 
        s += "&price=" + this.data.payMuch, s += "&goods_id=" + this.options.goods_id, s += "&orders=" + JSON.stringify(this.assembleDeduction())) : (s += a.getNetAddresss("plugin.store-cashier.frontend.Order.create"), 
        s += "&price=" + this.data.payMuch, s += "&store_id=" + this.data.store_id, s += "&member_coupon_ids=" + JSON.stringify(this.assembleCoupons()), 
        s += "&orders=" + JSON.stringify(this.assembleDeduction()), s += "&mobile=" + this.data.form.mobile, 
        s += "&realname=" + this.data.form.realname), 0 == this.data.clicktag) {
            if (this.setData({
                clicktag: 1
            }), this.data.agreementPayData && !this.data.agreementPay) return this.setData({
                clicktag: 0
            }), void wx.showToast({
                icon: "none",
                title: "请先勾选支付协议",
                duration: 1500
            });
            a._getNetWork({
                url: s,
                success: function(t) {
                    var a = t.data;
                    1 == a.result ? (i.setData({
                        order_ids: a.data.order_ids
                    }), i.getMultipleOrderInfo(e)) : (wx.showToast({
                        icon: "none",
                        title: a.msg,
                        duration: 1500
                    }), i.data.isPayClick = !1), setTimeout(function() {
                        i.setData({
                            clicktag: 0
                        });
                    }, 1e3);
                },
                fail: function(t) {
                    console.log(t);
                }
            });
        }
    },
    getMultipleOrderInfo: function(t) {
        var e = this, i = a.getNetAddresss("order.merge-pay");
        i += "&order_ids=" + this.data.order_ids, i += "&pid=" + this.data.uid, a._getNetWork({
            url: i,
            success: function(a) {
                var i = a.data;
                1 == i.result ? (e.setData({
                    order_sn: i.data.order_pay.pay_sn,
                    balance: i.data.member.credit2,
                    money: i.data.order_pay.amount,
                    uid: i.data.order_pay.uid,
                    order_pay_id: i.data.order_pay.id,
                    buttons: i.data.buttons,
                    min_redirect_url: i.data.min_redirect_url
                }), e.handlePay(t, t.value)) : (wx.showModal({
                    title: "提示",
                    content: i.msg,
                    success: function(t) {
                        t.confirm ? (wx.reLaunch({
                            url: "/packageG/index/index"
                        }), console.log("用户点击确定")) : t.cancel && console.log("用户点击取消");
                    }
                }), e.data.isPayClick = !1);
            },
            fail: function(t) {
                console.log(t);
            }
        });
    },
    getCenter: function() {
        var t = this, e = "", i = {};
        console.log(this.data.store_id, "getCenter的store_id"), "hotel" === this.data.cashier_type ? (i = {
            hotel_id: this.data.hotel_id
        }, e = a.getNetAddresss("plugin.hotel.frontend.cashier.center.index")) : (i = {
            store_id: this.data.store_id
        }, e = a.getNetAddresss("plugin.store-cashier.frontend.cashier.center.getPaySet")), 
        a._getNetWork({
            url: e,
            data: i,
            success: function(e) {
                var a = e.data;
                1 == a.result ? ("hotel" === t.data.cashier_type ? t.initStore(a.data.get_hotel_info) : t.initStore(a.data.get_store_info), 
                t.setData({
                    agreementPayData: a.data.get_pay_protocol
                })) : wx.showToast({
                    title: a.msg,
                    icon: "none",
                    duration: 1500
                });
            },
            fail: function(t) {
                console.log(t);
            }
        });
    }
});